
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <%--<c:choose>--%>
        <%--<c:when test="${type==0}"></c:when>--%>
        <%--<c:otherwise>--%>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <%--</c:otherwise>--%>
        <%--</c:choose>--%>

    </div>
</script>


<script type="text/html" id="barDemo">
    <c:choose>
    <c:when test="${sessionScope.user.utype==1}">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </c:when>
    <c:otherwise>
    <h4 style="color: red">您无权操作</h4>
    </c:otherwise>
    </c:choose>

</script>


<script src="${pageContext.request.contextPath}/layui/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use('table', function(){
        var table = layui.table;
        var pn ;//当前页
        var pc ;//当前页分几

        table.render({
            elem: '#test'
            ,url:'${pageContext.request.contextPath}/ProviderServlet.action?flag=findProviderAll'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户数据'
            ,cols: [[
                {type: 'checkbox', fixed: 'l条数据eft'}
                ,{field:'pid', title:'编号',  fixed: 'left', unresize: true, sort: true,align:'center'}
                ,{field:'pname', title:'公司姓名', edit: 'text'}
                ,{field:'people', title:'负责人姓名', edit: 'text'}
                ,{field:'phone', title:'电话',  edit: 'text', sort: true}
                ,{field:'address', title:'地址'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true,
            limit:3,
            request: {
                pageName: 'pageNumber' //页码的参数名称，默认：page
                ,limitName: 'pageCount' //每页数据量的参数名，默认：limit
            }
            ,response: {
                statusName: 'code'
                ,msgName:'msg'
                ,countName: 'total' //数据总数的字段名称，默认：count
                ,dataName: 'data' //数据列表的字段名称，默认：data
            },done:function(res, curr, count){
                pc = res.pageCount; //得到当前页分几条数据
                pn = curr ;//得到当前页
            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
                case 'add':
                    layer.open({
                        type: 1 //Page层类型
                        ,skin: 'layui-layer-molv'
                        ,area: ['500px', '500px']
                        ,title: ['添加类别信息','font-size:18px']
                        ,btn: ['确定', '取消']
                        ,shadeClose: true
                        ,shade: 0 //遮罩透明度
                        ,maxmin: true //允许全屏最小化
                        ,content:$('#box1')  //弹窗id
                        ,success:function(layero,index){
                            $("#box1")[0].reset()//重置form
                        },yes:function(index,layero){

                            $.post("${pageContext.request.contextPath}/ProviderServlet.action?flag=add",$("#box1").serialize(),function(data){
                                if(data){
                                    layer.alert('添加成功',{icon:1,title:'提示'},function(i){
                                        layer.close(i);
                                        layer.close(index);//关闭弹出层
                                        $("#box1")[0].reset()//重置form
                                    })
                                    table.reload('test',{//重载表格
                                        page:{
                                            curr:1,
                                            count:pc,
                                        }
                                    })
                                }
                            },"json");
                        }
                    });
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    //obj.del();//删除此条数数
                    $.post("${pageContext.request.contextPath}/ProviderServlet.action?flag=deleteById","pid="+data.pid,function(data){
                        console.log(pn);
                        console.log(pc);
                        if(data.flag){
                            alert("成功");
                            table.reload('test',{//重载表格
                                page:{
                                    curr:pn,
                                    count:pc
                                }
                            })
                        }else{
                            alert("失败");
                        }
                    },"json");
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 1 //Page层类型
                    ,skin: 'layui-layer-molv'
                    ,area: ['500px', '500px']
                    ,title: ['编辑用户信息','font-size:18px']
                    ,btn: ['确定', '取消']
                    ,shadeClose: true
                    ,shade: 0 //遮罩透明度
                    ,maxmin: true //允许全屏最小化
                    ,content:$('#box1')  //弹窗id
                    ,success:function(layero,index){
                        $('#pid').val(data.pid);
                        $('#pname').val(data.pname);
                        $('#people').val(data.people);
                        $('#phone').val(data.phone);
                        $('#address').val(data.address);

                    },yes:function(index,layero){
                        $.post("${pageContext.request.contextPath}/ProviderServlet.action?flag=updateById",$("#box1").serialize(),function(data){
                            //根据后台返回的参数，来进行判断
                            if(data.flag){
                                layer.alert('编辑成功',{icon:1,title:'提示'},function(i){
                                    layer.close(i);
                                    layer.close(index);//关闭弹出层
                                    $("#box1")[0].reset()//重置form
                                })
                                table.reload('test',{//重载表格
                                    page:{
                                        curr:pn,
                                        count:pc
                                    }
                                })
                            }

                        },"json");
                    }
                });
            }
        });

    });

</script>


<form class="layui-form" action="${pageContext.request.contextPath}/StudentServlet.action?flag=update" id="box1" style="display: none;">
    <input name="pid" id="pid" type="hidden">

    <div class="layui-form-item">
        <label class="layui-form-label">姓名:</label>
        <div class="layui-input-block">
            <input type="text" name="pname" id="pname" lay-verify="title" autocomplete="off" placeholder="请输入姓名" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">负责人:</label>
        <div class="layui-input-block">
            <input type="text" name="people" id="people" lay-verify="title" autocomplete="off" placeholder="请输入负责人" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">地址:</label>
        <div class="layui-input-block">
            <input type="text" name="address" id="address" lay-verify="title" autocomplete="off" placeholder="请输入地址" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">手机号:</label>
        <div class="layui-input-block">
            <input type="text" name="phone" id="phone" lay-verify="title" autocomplete="off" placeholder="请输入手机号" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">注册资金:</label>
        <div class="layui-input-block">
            <input type="text" name="pmoney" id="pmoney" lay-verify="title" autocomplete="off" placeholder="请输入注册资金" class="layui-input">
        </div>
    </div>

</form>


</body>
</html>

<script>
    var form;
    var layer;
    layui.use(['form', 'layedit', 'laydate'], function(){
        var laydate = layui.laydate;
        form = layui.form;
        layer = layui.layer;

        //日期
        laydate.render({
            elem:'#ubirth'
        });
    });
</script>